package main

import (
	"context"
	"embed"
	os_runtime "runtime"

	"github.com/wailsapp/wails/v2"
	"github.com/wailsapp/wails/v2/pkg/menu"
	"github.com/wailsapp/wails/v2/pkg/menu/keys"
	"github.com/wailsapp/wails/v2/pkg/options"
	"github.com/wailsapp/wails/v2/pkg/options/assetserver"
	"github.com/wailsapp/wails/v2/pkg/runtime"
)

//go:embed all:frontend/dist
var assets embed.FS

func main() {
	// Create an instance of the app structure
	app := NewApp()
	// Instantiate other structures here
	shareApp := NewShareApp()
	httpApp := NewHTTPApp()

	AppMenu := menu.NewMenu()
	FileMenu := AppMenu.AddSubmenu("File")
	// FileMenu.AddText("&Open", keys.CmdOrCtrl("f"), func(_ *menu.CallbackData) {
	// 	// runtime.EventsEmit(app.ctx, "open-dialog", "open")
	// 	fmt.Println("Open")
	// })
	FileMenu.AddSeparator()
	FileMenu.AddText("Quit", keys.CmdOrCtrl("q"), func(_ *menu.CallbackData) {
		runtime.Quit(app.ctx)
	})

	if os_runtime.GOOS == "darwin" {
		AppMenu.Append(menu.EditMenu())
	}

	// Create application with options
	err := wails.Run(&options.App{
		Title:  "dev_tools",
		Width:  1024,
		Height: 768,
		AssetServer: &assetserver.Options{
			Assets: assets,
		},
		BackgroundColour: &options.RGBA{R: 27, G: 38, B: 54, A: 1},
		OnStartup: func(ctx context.Context) {
			log_.Debug("Wails app startup...")
			app.startup(ctx)
			// Bind other structures context here.
			shareApp.startup(ctx)
			httpApp.startup(ctx)
		},
		OnShutdown: func(ctx context.Context) {
			// Shutdown other structures here.
			app.shutdown(ctx)
			shareApp.shutdown()
			httpApp.shutdown()
		},
		Bind: []interface{}{
			app,
			// Register other structures here.
			shareApp,
			httpApp,
		},
		// Menu: AppMenu,
	})

	if err != nil {
		log_.Errorf("Error: %s", err.Error())
	} else {
		log_.Debug("Wails app shutdown successfully.")
	}
}
